Thread: Double pointer final, printing final[0] garbaje after allocating memory to final[4]

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    18

    Double pointer final, printing final[0] garbaje after allocating memory to final[4]

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int ulen,slen,mergelen;
    char **final;
    int overlap_last(char *st)
    {
      int i,k,j,length;
      char last;
      int overlap_at;
      overlap_at=0;
      length=strlen(st);
      last=st[length-1];
      for(k=0;k<=((length-3)/2);k++)
       {
         if(st[length-1-k-1]==last && st[length-1-k-1-k-1]==last)
           {
             overlap_at=length-1-k-1;
             for(j=0;j<k;j++)
               {
                if(st[length-1-k-1-k+j]!=st[length-1-k+j])
                  {
                    overlap_at=0;
            break;
                  }
               }
              if(overlap_at!=0)
                 break;
           }  
       }
      return overlap_at;
    }
    
    void overlapfree_merge_sets(char **u, int ulen, char **s, int slen)
    {
      int i,j,k,overlap,len,l,p,duplicate,finallen,length;
      char *temp;
      char *temp1;
      overlap=0;
      duplicate=0;
      final=(char **)malloc(sizeof(char *)*ulen);
      for(i=0;i<ulen;i++)
        {
          length=strlen(u[i]);
          final[i]=(char *)malloc(length*sizeof(char));
          strcpy(final[i],u[i]);
        } 
      finallen=ulen;
      for(i=0;i<ulen;i++)
        {   
          for(j=0;j<slen;j++)
            { 
              length=strlen(u[i]);
              temp=(char *)malloc(length*sizeof(char));
              strcpy(temp,u[i]);
              len=strlen(temp);
              strcat(temp,s[j]);
              overlap=0;
              for(k=len+1;k<=strlen(temp);k++)
                {
                  temp1=(char *)malloc(sizeof(char)*k);
                  strncpy(temp1,temp,k);
                  if(overlap_last(temp1)) 
                    {
                       overlap=1;
                       free(temp1);
                       break;
                    }
                  free(temp1);
                }
              //adding overlap free concatenated element of u[i] and s[j]
              if(overlap!=1 && k==(strlen(temp)+1))
                {
                   duplicate=0;
                   for(l=0;l<finallen;l++)
                    {
                       if(strcmp(final[l],temp)==0) 
                         {
                           duplicate=1;
                           break;
                         }
                       else duplicate=0;
                    }
                  if(duplicate!=1)
                    {
                       length=strlen(temp);
                       final[finallen]=(char *)malloc(length*sizeof(char));
                       strcpy(final[finallen],temp);
                       finallen++;
                    }
                }
              free(temp);
              ///adding all element of s
              if(i==0)
                {
                  duplicate=0;
                  for(l=0;l<finallen;l++)
                    {
                      if(strcmp(final[l],s[j])==0) 
                        {
                           duplicate=1;
                           break;
                         }
                      else duplicate=0;
                    }              
                  if(duplicate!=1)
                    {
                      length=strlen(s[j]);
                      final[finallen]=(char *)malloc(length*sizeof(char));
                      strcpy(final[finallen],s[j]);
                      finallen++;
                    }
                }  
            }  //s loop
        }///u loop 
      mergelen=finallen;
    }
    
    int main()
    { 
    int i,k;
    char **u;
    char **s;
    char **t;
    ulen=2;
    slen=2;
    
    u=(char **)malloc(sizeof(char *)*ulen);
    for(i=0;i<ulen;i++)
      {
        printf("k=");
        scanf("%d",&k);
        u[i]=(char *)malloc(sizeof(char)*k);
        printf("u[%d]=",i);
        scanf("%s",u[i]);
      }
    
    printf("\n");
    
    s=(char **)malloc(sizeof(char *)*slen);
    for(i=0;i<slen;i++)
      {
        printf("k=");
        scanf("%d",&k);
        s[i]=(char *)malloc(sizeof(char)*k);
        printf("s[%d]=",i);
        scanf("%s",s[i]);
    }
    
    overlapfree_merge_sets(u,ulen,s,slen);
    
    for(i=0;i<mergelen;i++)
      {
        printf("%s\n",final[i]);
    
      }
    
      printf("\n");
      return 0;
    } 
    
     When you run above programme enter value like below:
    
    k=1
    u[0]=a
    k=1
    u[1]=b
    
    k=1
    s[0]=a
    k=1
    s[1]=b
    
    output:
    
    �A�                          it should be a
    b
    aa
    ab
    ba
    bb
    
    Logic of programme:
    Suppose, we have given two sets of strings, e.g. u={a,b} and s={a,b}.  These set might be different. Generate all possible concatenation of  element of u followed by element of s (u and s contains epsilon as a  element), e.g. output final={a,b,aa,ab,ba,bb} for above example. Out of  those concatenation only some are acceptable in the final set. Means,  concatenated element should satisfy some specific criteria.That criteria  we are checking in overlap_last function.
    
    Another example:
    u={a,b,aa,bb}
    s={a}
    final={a,b,aa,ba,bba} aaa is not satisfying our criteria.
    Overlap last function is working correct. 
    
    My problemi:
    output is stored in global variable final.
    Except final[0] all final[i] are correct.
    It is working fine according to my logic.
    But, some where memory problem is occurring.
    
    I had debug this programme.
    Every thing is working correct untill value of finallen=3, correct final[0]=a.
    After finallen=4 and when programme execute this below line it assigns final[0]=�A�. 
    final[finallen]=(char *)malloc(length*sizeof(char));
     
    if i increase set size then it gives some more values like above, then  other correct values.  But, it should print correct values corresponding  to final[i], instead of printing values like �A�.
    
    Please help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Only the program should go between the code tags, not your actual question.

    Code:
    u[i]=(char *)malloc(sizeof(char)*k);
    This is juuuuuuust one character too short to store the string (assuming k is supposed to represent the length of the string you are about to type in, although I admit that's a pretty big assumption to make of something called "k"). You do this repeatedly, so all your null characters are either overwriting things you want or getting overwritten by other things, and so heaven only knows what your program actually thinks the strings are by the time you're done.

    Also, why are you casting malloc?

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    18
    Code:
     #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int ulen,slen,mergelen;
    char **final;
    int overlap_last(char *st)
    {
      int i,k,j,length;
      char last;
      int overlap_at;
      overlap_at=0;
      length=strlen(st);
      last=st[length-1];
      for(k=0;k<=((length-3)/2);k++)
       {
         if(st[length-1-k-1]==last && st[length-1-k-1-k-1]==last)
           {
             overlap_at=length-1-k-1;
             for(j=0;j<k;j++)
               {
                if(st[length-1-k-1-k+j]!=st[length-1-k+j])
                  {
                    overlap_at=0;
            break;
                  }
               }
              if(overlap_at!=0)
                 break;
           }  
       }
      return overlap_at;
    }
    void overlapfree_merge_sets(char **u, int ulen, char **s, int slen)
    {
      int i,j,k,overlap,len,l,p,duplicate,finallen,length;
      char *temp;
      char *temp1;
      overlap=0;
      duplicate=0;
      final=(char **)malloc(sizeof(char *)*ulen);
      for(i=0;i<ulen;i++)
        {
          length=strlen(u[i]);
          final[i]=malloc((length+1)*sizeof(char));
          strcpy(final[i],u[i]);
        } 
      finallen=ulen;
      for(i=0;i<ulen;i++)
        {   
          for(j=0;j<slen;j++)
            { 
              length=strlen(u[i]);
              temp=malloc((length+1)*sizeof(char));
              strcpy(temp,u[i]);
              len=strlen(temp);
              strcat(temp,s[j]);
              overlap=0;
              for(k=len+1;k<=strlen(temp);k++)
                {
                  temp1=malloc(sizeof(char)*(k+1));
                  strncpy(temp1,temp,k);
                  if(overlap_last(temp1)) 
                    {
                       overlap=1;
                       free(temp1);
                       break;
                    }
                  free(temp1);
                }
              //adding overlap free concatenated element of u[i] and s[j]
              if(overlap!=1 && k==(strlen(temp)+1))
                {
                   duplicate=0;
                   for(l=0;l<finallen;l++)
                    {
                       if(strcmp(final[l],temp)==0) 
                         {
                           duplicate=1;
                           break;
                         }
                       else duplicate=0;
                    }
                  if(duplicate!=1)
                    {
                       length=strlen(temp);
                       final[finallen]=malloc((length+1)*sizeof(char));
                       strcpy(final[finallen],temp);
                       finallen++;
                    }
                }
              free(temp);
              ///adding all element of s
              if(i==0)
                {
                  duplicate=0;
                  for(l=0;l<finallen;l++)
                    {
                      if(strcmp(final[l],s[j])==0) 
                        {
                           duplicate=1;
                           break;
                         }
                      else duplicate=0;
                    }              
                  if(duplicate!=1)
                    {
                      length=strlen(s[j]);
                      final[finallen]=malloc((length+1)*sizeof(char));
                      strcpy(final[finallen],s[j]);
                      finallen++;
                    }
                }  
            }  //s loop
        }///u loop 
      mergelen=finallen;
    }
    int main()
    { 
    int i,k;
    char **u;
    char **s;
    char **t;
    ulen=2;
    slen=2;
    u=(char **)malloc(sizeof(char *)*ulen);
    for(i=0;i<ulen;i++)
      {
        printf("k=");
        scanf("%d",&k);
    //    u[i]=(char *)malloc(sizeof(char)*(k+1));
        u[i]=malloc(sizeof(char)*(k+1));
        printf("u[%d]=",i);
        scanf("%s",u[i]);
      }
    printf("\n");
    s=(char **)malloc(sizeof(char *)*slen);
    for(i=0;i<slen;i++)
      {
        printf("k=");
        scanf("%d",&k);
    //    s[i]=(char *)malloc(sizeof(char)*(k+1));
        s[i]=malloc(sizeof(char)*(k+1));
        printf("s[%d]=",i);
        scanf("%s",s[i]);
    }
    overlapfree_merge_sets(u,ulen,s,slen);
    for(i=0;i<mergelen;i++)
      {
        printf("%s\n",final[i]);
      }
      printf("\n");
      return 0;
    }
    I have changed code.
    But it is giving same error.

    Mathematical Logic:
    Enter set of strings u
    Enter set of strings s
    Final set:
    Code:
     final= u union s union {xy | x element of u, and y is element of s, and for ALL yi overlap_last(xyi) should be 0, where yi is prefix of y  }
    The overlap last function is defined in programme.
    The overlap_last function is working corect.

    When you run above programme enter value like below:
    Code:
    k=1
    u[0]=a
    k=1
    u[1]=b
    
    k=1
    s[0]=a
    k=1
    s[1]=b
    output:
    Code:
    �A�                          it should be a
    b
    aa
    ab
    ba
    bb
    if you want to change length of u and s, then change ulen and slen, for above input it is defined as ulen=2 ans slen=2.
    Last edited by mrityunjay23; 10-18-2013 at 10:47 PM.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Since in your final array you allocate only ulen pointers
    the lines 108-110 cause out-of-bounds access to the memory...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    18
    It has resolved. I was not able to realise this mistake. Extremely Thank You

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX dll Final Debug and Final Retail
    By hdragon in forum Tech Board
    Replies: 0
    Last Post: 11-15-2005, 09:46 PM
  2. The final frontier
    By DISGUISED in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 01-15-2004, 02:30 PM
  3. 2.6-final
    By -KEN- in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 12-23-2003, 10:24 PM
  4. Help with FINAL assignment
    By ray in forum C++ Programming
    Replies: 16
    Last Post: 01-11-2003, 02:54 AM
  5. Help With Final
    By Matt123456 in forum C++ Programming
    Replies: 10
    Last Post: 05-08-2002, 06:44 PM

Tags for this Thread